package com.glink.manage.controller;

import com.glink.common.util.BaseCommonUtils;
import com.glink.manage.domain.SilentResultBean;
import com.glink.manage.domain.VehicleInfoBean;
import com.glink.manage.dto.barriergate.lianyi.CarBarrierGateListAddDataDTO;
import com.glink.manage.dto.mqtt.CarBarrierGateListDeleteDataDTO;
import com.glink.manage.dto.vehicle.VehicleDataHandleDTO;
import com.glink.manage.service.SilentResultService;
import com.glink.manage.service.VehicleService;
import com.glink.manage.service.barriergate.BarrierGate_LianYi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 文档资料和附件等上传
 *
 * @author rencl
 */
@Slf4j
@RestController
@RequestMapping("/API/Manage/data/handle")
@Api(tags = "数据处理")
public class DataHandleController extends AbstractBaseController {
    
    @Autowired
    private VehicleService vehicleService;

    @Autowired
    private BarrierGate_LianYi lianYi;
    
    @Autowired
    private SilentResultService silentResultService;

    @ApiOperation(value = "车辆道闸-下发用户")
    @PostMapping(value = "/listAdd", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void listAdd(@Validated @RequestBody VehicleDataHandleDTO dto) throws Exception {

        VehicleInfoBean vehicleInfoBean = vehicleService.getById(dto.getObjId());
        if(Objects.nonNull(vehicleInfoBean)){
            CarBarrierGateListAddDataDTO addDataDTO = new CarBarrierGateListAddDataDTO();
            addDataDTO.setPlatenum(vehicleInfoBean.getLicensePlate());
            addDataDTO.setStart(dto.getType());
            addDataDTO.setStart("2024/01/01 00:00:00");
            addDataDTO.setEnd("2025/12/01 00:00:00");

            lianYi.listAdd("14ZI251829268102", Collections.singletonList(addDataDTO));
        }
    }

    @ApiOperation(value = "车辆道闸-名单删除")
    @PostMapping(value = "/listDelete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void listDelete(@Validated @RequestBody VehicleDataHandleDTO dto) throws Exception {
        VehicleInfoBean vehicleInfoBean = vehicleService.getById(dto.getObjId());
        if(Objects.nonNull(vehicleInfoBean)){
            CarBarrierGateListDeleteDataDTO deleteDataDTO = new CarBarrierGateListDeleteDataDTO();
            deleteDataDTO.setPlatenum(vehicleInfoBean.getLicensePlate());

            lianYi.listDelete("14ZI251829268102", Collections.singletonList(deleteDataDTO));
        }
    }

    @ApiOperation(value = "车辆道闸-名单查询")
    @PostMapping(value = "/listQuery", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void listQuery(@Validated @RequestBody VehicleDataHandleDTO dto) throws Exception {
        VehicleInfoBean vehicleInfoBean = vehicleService.getById(dto.getObjId());
        if(Objects.nonNull(vehicleInfoBean)){

            lianYi.listQuery("14ZI251829268102", vehicleInfoBean.getLicensePlate());
        }
    }


    @ApiOperation(value = "下载图片")
    @GetMapping(value = "/downImage", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void downImage() throws Exception {
        
        String str = "2025-02-25 07:53:30";
        Date parse = BaseCommonUtils.DATE_FORMAT_SECOND_0.parse(str);
        

        List<SilentResultBean> resultBeanList = silentResultService.lambdaQuery()
//                .like(SilentResultBean::getEquipId, "daf22c9be90047b9990ed82030403bf8")
                .ge(SilentResultBean::getCreateTime, parse)
                .list();
        if(CollectionUtils.isNotEmpty(resultBeanList)){
            resultBeanList.forEach(resultBean -> {
//                OutputStream outputStream = null;
//                try {
//                    byte[] ftpFileByte = ftpUtils.getFtpFileByte(resultBean.getFilePath());
//                    if (null != ftpFileByte && ftpFileByte.length > 0) {
//                        // 文件路径
//                        Path filePath = Paths.get("E:\\qss\\test\\image\\" + FilenameUtils.getName(resultBean.getFilePath()));
//
//                        try {
//                            Files.write(filePath, ftpFileByte);
//                            System.out.println("File created successfully.");
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                } catch (Exception e) {
//                    log.error("ftp下载文件失败,文件路径：{}", resultBean.getFilePath(), e);
//                } finally {
//                    IOUtils.closeQuietly(outputStream);
//                }
            });
        }

    }

//    @ApiOperation(value = "手动修改系统授权到期,yyyy-mm-dd")
//    @GetMapping(value = "/authExpires/{dateStr}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public void authExpires(@PathVariable("dateStr") String dateStr) throws Exception {
//        if(StringUtils.isNotBlank(dateStr)){
//            GrantCodeInterceptor.periodValidity = BaseCommonUtils.DATE_FORMAT_DAY_3.parse(dateStr);
//        }else{
//            GrantCodeInterceptor.periodValidity = null;
//        }
//    }
}